//
//  Present.m
//  Advance_MVC
//
//  Created by Willian on 2021/5/2.
//

#import "Present.h"
#import "MVCMainView.h"

@interface Present ()

@property (nonatomic, copy) DataLoadSuccessBlock dataLoadSuccessBlock;
@property (nonatomic, strong) MVCMainView *mainView;

@property (nonatomic, strong) NSMutableArray *resArr;

@end

@implementation Present

- (instancetype)initWithMainView:(MVCMainView *)mainView  withDataLoadSuccessBlock:(DataLoadSuccessBlock)dataLoadSuccessBlock{
    self = [super init];
    if (self) {
        self.mainView = mainView;
        self.dataLoadSuccessBlock = dataLoadSuccessBlock;
        
        //实例化的时候，直接请求
        [self asyncLoadData];
    }
    return self;
}

- (void)asyncLoadData{
    //模拟异步加载数据，完成后回调
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *tmpArr = @[
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)},
            @{@"name":@"张三",@"gender":@"男",@"age":@(18)}
        ];
        //数据转模型
        for (NSDictionary *tmpDic in tmpArr) {
            UserModel *userModel = [[UserModel alloc] init];
            [userModel setValuesForKeysWithDictionary:tmpDic];
            [self.resArr addObject:userModel];
        }
        
        //接口调完成，这里要回调刷界面
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //这里调用数据请求完后的回调
            if (self.dataLoadSuccessBlock) {
                self.dataLoadSuccessBlock(self.resArr);
            }
            //这里直接刷UI
            [self.mainView reloadData];
        });
    });
}

#pragma mark - PresentDelegate
//UI -> model
- (void)changeUserAge:(int)age withIndex:(NSInteger)index{
    @autoreleasepool {
        if (index < self.resArr.count) {
            UserModel *tmpModel = self.resArr[index];
            tmpModel.age = age;
            //因为是引用类型，下面这句话可以不要，自己去理解
            //self.resArr[index] = tmpModel;
        }
    }
}

//点击了某一行cell  UI -> Model
- (void)selectCellWithIndex:(NSInteger)index{
    NSLog(@"选中了某一行 %ld", index);
}

//model -> UI
- (void)obtainDataAfterResreshUI:(MVCMainView *)mainView{
    //刷界面
    [mainView reloadData];
}

#pragma mark - 数据懒加载
- (NSMutableArray *)resArr{
    if (!_resArr) {
        _resArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _resArr;
}

@end
